/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BUS;

import DAO.DAO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Endlesslove
 */
public class PhieuThuTien implements IDaTa{

    @Override
    public ResultSet Search(DTO.Idata Obj) 
    {
//        try
//        {
//            List<String> params = new ArrayList<>();
//            params.add("char:" +  param[0]) ;//masach
//            return DAO.CallProc("LayThongTinHoaDon", params);
//            
//        } 
//        catch (Exception ex) 
//        {
//            return null;
//        }
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean Update(DTO.Idata Obj) 
    {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public boolean Delete(DTO.Idata Obj)
    {
        List<String> params = new ArrayList<>();
        params.add("char:" + ((DTO.PhieuThuTien)Obj).getID());
        return DAO.CallProc("XoaPhieuThuTien", params, true);
    }

    @Override
    public void LoadData(Object table) 
    {
         try
        {
            ResultSet res = DAO.CallProc("LayThongTinToanBoPhieuThu");
            int count = 1;
            while (res.next()) 
            {
                ((DefaultTableModel)((JTable)table).getModel()).addRow(new Object[] 
                {
                    count++ ,res.getObject(1),res.getObject(2),res.getObject(3),res.getObject(4),res.getObject(5),res.getObject(6),res.getObject(7)
                });
            }
        }
        catch (SQLException ex) 
        {
            System.err.println(ex.getMessage());
        }  
    }

    @Override
    public boolean Insert(DTO.Idata Obj)
    {
        List<String> params = new ArrayList<>();
        params.add("char:" +  ((DTO.PhieuThuTien)Obj).getID()) ;//ma phieu thu
        params.add("char:" +  ((DTO.PhieuThuTien)Obj).MaKhachHang);//Ma Khach Hang
        params.add("Date:" +  ((DTO.PhieuThuTien)Obj).NgayThuTien);// Ngay Thu Tien
        params.add("int:" +  ((DTO.PhieuThuTien)Obj).SoTienThu );//So Tien Thu
        return DAO.CallProc("ThemPhieuThuTien", params, true);
    }

    @Override
    public String GenerateID()
    {
        try 
        {
                ResultSet res = DAO.CallProc("LayMaPhieuThuCuoi");
                if (res.next())
                {    
                  int index  = Integer.parseInt(((String)res.getObject(1)).split("PT")[1].trim());
                  String mkh = "PT";
                  mkh += (index+1)>=10?(index+1)>=100? (index +1) : "0" + (index +1) : "00" + (index + 1);
                  return mkh;
                }
                return "PT001";
        } 
        catch (SQLException ex) 
        {
            return "PT001";
        }
        catch (Exception ie)
        {
            return "PT001";
        }
    }
    
    public boolean DeleteHD(String MHD)
    {
        try
        {
            List<String> params = new ArrayList<>();
            params.add("char:" +  MHD) ;//masach
            return DAO.CallProc("XoaPhieuThuTien", params, true);
            
        } 
        catch (Exception ex) 
        {
            return false;
        }
    }
    
}
